<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20"><svg-icon icon-class="home" /><span>传值博客有限公司</span></el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <el-col>负责人</el-col>
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置el-tree组件 -->
        <el-tree
          :data="list"
          default-expand-all
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20"><svg-icon icon-class="home" /><span>{{ data.name }}</span></el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="deldit(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template><el-tree /></el-tree></el-card>
    </div>
    <el-dialog
      v-if="showDialog"
      title="添加或编辑"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <template>
        <dept
          :id="curId"
          :origin-list="originList"
          :edit="isEdit"
          @close="showDialog=false"
          @success="hsuccess"
        />
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/tranListToTreeData'
import dept from './deptDialog.vue'
export default {
  components: {
    dept
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []// 传递给子组件的数据
    }
  },
  created() {
    this.loadgetDepartment()
  },
  methods: {
    // 渲染树形数据
    async loadgetDepartment() {
      const res = await getDepartments()
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          code: item.code,
          name: item.name
        }
      })
      console.log('res', res)
      res.data.depts.shift()
      this.list = tranListToTreeData(res.data.depts)
    },
    // 添加功能
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 编辑功能
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    // 删除功能
    deldit(id) {
      this.$confirm('您确定要删除吗').then(async() => {
        try {
          const res = await delDepartment(id)
          console.log('要删除的部门', res)
          if (!res.code === 10000) return
          this.$message.success(res.message)
          this.loadgetDepartment()
        } catch (err) {
          console.log(err)
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 关闭弹层,再次更新数据
    hsuccess() {
      this.showDialog = false
      this.loadgetDepartment()
    }
  }
}

</script>
